package com.funmi.lizip.security.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.funmi.lizip.entity.AdminUser;
import com.funmi.lizip.mapper.AdminUserMapper;
import com.funmi.lizip.security.entity.SecurityAdminUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;

@Slf4j
public class AdminUserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private AdminUserMapper adminUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 从数据源中读取用户信息
        // AdminUserMapper adminUserMapper = new AdminUserMapper();
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                .eq("is_delete", 0);
        AdminUser user = adminUserMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new UsernameNotFoundException(String.format("用户['%s']不存在", username));
        }

        /*// 读取用户的所有角色
        List<Role> roleList = userMapper.selectRoleForUsername(user.getId());
        // 读取用户的所有权限
        List<Permission> permissionList = userMapper.selectPermissionForUsername(user.getId());
		// 构造security用户
		return new SecurityUser(user, roleList, permissionList)*/;
        return new SecurityAdminUser(user, null, null);
    }
}